package com.shijunhao.graduation.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shijunhao.graduation.annotations.OperationLog;
import com.shijunhao.graduation.domain.bean.ResultCode;
import com.shijunhao.graduation.domain.dto.BusinessSourcesDto;
import com.shijunhao.graduation.domain.dto.BusinessSourcesQueryDto;
import com.shijunhao.graduation.domain.entity.BusinessSources;
import com.shijunhao.graduation.domain.vo.BusinessSourceInfoVo;
import com.shijunhao.graduation.domain.vo.BusinessSourcesListVo;
import com.shijunhao.graduation.domain.vo.BusinessSourcesVo;
import com.shijunhao.graduation.properties.OperationTarget;
import com.shijunhao.graduation.properties.OperationType;
import com.shijunhao.graduation.service.business.IBusinessSourcesService;
import com.shijunhao.graduation.service.business.IBusinessUserInfoService;
import com.shijunhao.graduation.utils.PageUtils;
import com.shijunhao.graduation.utils.WrapperUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author shijunhao
 * @since 2022-04-16
 */
@RestController
@RequestMapping("/business-sources")
@Api(tags = "资源共享")
public class BusinessSourcesController {

    @Resource
    private IBusinessSourcesService businessSourcesService;
    @Resource
    private IBusinessUserInfoService businessUserInfoService;

    @PostMapping("insert")
    @ApiOperation("新增")
    @OperationLog(target = OperationTarget.SOURCE,type = OperationType.INSERT,description = "新增资源")
    public ResultCode<Long> insert(@RequestBody BusinessSourcesDto dto) {
        Long insert = businessSourcesService.insert(dto);
        if (insert != null)
            return ResultCode.success("插入成功", insert);
        else
            return ResultCode.error("插入失败");
    }


    @GetMapping("list")
    @ApiOperation("私有列表查询")
    public ResultCode<IPage<BusinessSourcesVo>> page(@ModelAttribute BusinessSourcesQueryDto query) {
        Long id = businessUserInfoService.getCurrentUser().getId();
        query.setCreateBy(id);
        Page<BusinessSources> page = businessSourcesService.page(PageUtils.getPage(), WrapperUtils.query(query));
        return ResultCode.success(page.convert(businessSourcesService::toVo));
    }

    @GetMapping("listAll")
    @ApiOperation("公有列表查询")
    public ResultCode<IPage<BusinessSourcesListVo>> allPage(@ModelAttribute BusinessSourcesQueryDto query) {
        Page<BusinessSources> page = businessSourcesService.page(PageUtils.getPage(), WrapperUtils.query(query));
        return ResultCode.success(page.convert(businessSourcesService::toListVo));
    }


    @PostMapping("delete/{id}")
    @ApiOperation("删除")
    @OperationLog(target = OperationTarget.POST,type = OperationType.DELETE,description = "删除资源")
    public ResultCode<Boolean> delete(@PathVariable Long id) {
        boolean delete = businessSourcesService.deleteById(id);
        if (delete)
            return ResultCode.success("删除成功");
        else
            return ResultCode.error("删除失败");
    }

    @PostMapping("update")
    @ApiOperation("更新")
    @OperationLog(target = OperationTarget.POST,type = OperationType.UPDATE,description = "更新资源")
    public ResultCode<Boolean> update(@RequestBody BusinessSourcesDto dto) {
        BusinessSources businessSources = businessSourcesService.getById(dto.getId());
        BeanUtils.copyProperties(dto, businessSources);
        boolean update = businessSourcesService.update(businessSources);
        if (update)
            return ResultCode.success("更新成功");
        else
            return ResultCode.error("更新失败");
    }

    @GetMapping("info/{id}")
    @ApiOperation("详情")
    public ResultCode<BusinessSourceInfoVo> info(@PathVariable Long id){
        BusinessSources entity = businessSourcesService.getById(id);
        if(entity != null){
            BusinessSourceInfoVo mapping = businessSourcesService.toInfoVo(entity);
            return ResultCode.success(mapping);
        }else{
            return ResultCode.error("没有找到这条信息。");
        }
    }
}
